/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entities;

import bookmaker.HibernateUtil;
import com.icesoft.faces.component.menubar.MenuItem;
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author ALIEN
 */
public class Discipline {

    private long id;
    private String name;
    private String image_path;
    private Session session;
    private SelectItem[] disciplines;
    private List model = new ArrayList();
    private final String URL = new String("showGames.iface");


    /**
     * @return the id
     */
    public long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }


    public String dodaj() {
        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            setSession(sessionFactory.openSession());
            Transaction tx = getSession().beginTransaction();
            getSession().save(this);
            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            getSession().close();
        }
        return "ok";
    }

    public SelectItem[] getDisciplines() {
        ArrayList disciplineList = new ArrayList();

        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

            setSession(sessionFactory.openSession());
            Transaction tx = getSession().beginTransaction();
            Query q = getSession().createQuery("from Discipline");
            disciplineList = (ArrayList) q.list();
            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (getSession() != null) {
                getSession().close();
            }
        }

        disciplines = new SelectItem[disciplineList.size()];
        for (int i = 0; i < disciplineList.size(); i++) {
            disciplines[i] = new SelectItem(disciplineList.get(i), ((Discipline) (disciplineList.get(i))).name);
        }
        return disciplines;
    }

    @Override
    public boolean equals(Object obj) {
        return true;
    }

    public List getModel() {
        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

            setSession(sessionFactory.openSession());
            Transaction tx = getSession().beginTransaction();
            Query q = getSession().createQuery("from Discipline");
            ArrayList temp = (ArrayList) q.list();
            model.clear();
            List subTemp;
            for(Object o:temp){
                MenuItem item = new MenuItem();
                item.setValue(((Discipline)o).getName());
                long id = ((Discipline)o).getId();
                Query subQ = getSession().createQuery("select distinct l from League l left join fetch l.discipline as disc where disc.id ="+id);
                subTemp = subQ.list();
                for(Object subO:subTemp){
                    MenuItem subItem = new MenuItem();
                    subItem.setValue(((League)subO).getName());
                    id = ((League)subO).getId();
                    subItem.setLink(URL+"?g="+id);                  
                    item.getChildren().add(subItem);
                }
                model.add(item);
            }
            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (getSession() != null) {
                getSession().close();
            }
        }
        return model;
    }

    /**
     * @return the image_path
     */
    public String getImage_path() {
        return image_path;
    }

    /**
     * @param image_path the image_path to set
     */
    public void setImage_path(String image_path) {
        this.image_path = image_path;
    }

    /**
     * @return the session
     */
    public Session getSession() {
        return session;
    }

    /**
     * @param session the session to set
     */
    public void setSession(Session session) {
        this.session = session;
    }

}
